﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TooltracePC
{
    public partial class frmUser : Form
    {
        dgv dgvObject = new dgv();

        public frmUser()
        {
            InitializeComponent();
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            //this.Hide();
            this.Close();
        }

        private void frmUser_Load(object sender, EventArgs e)
        {
            int UserID = dgvObject.getSelectID();

            if (UserID > 0)
            {
                dbUser UserDb = new dbUser();
                DataTable dtTool = null;
                dtTool = UserDb.getByID(UserID);
                btnOK.Text = "更新";
                this.Text = "更新员工信息";
                if (dtTool != null)
                {
                    tbLableID.Text = dtTool.Rows[0]["LableID"].ToString();
                    tbUserName.Text = dtTool.Rows[0]["UserName"].ToString();
                    tbUserNo.Text = dtTool.Rows[0]["UserNo"].ToString();
                    tbUserPass.Text = dtTool.Rows[0]["UserPass"].ToString();
                }

            }
            else
            {
                btnOK.Text = "确定";
                this.Text = "添加员工";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int UserID = dgvObject.getSelectID();
            int i = 0;

            string LableID, UserName, UserNo, UserPass;

            dbUser UserDb = new dbUser();

            LableID = tbLableID.Text;
            UserName = tbUserName.Text;
            UserNo = tbUserNo.Text;
            UserPass = tbUserPass.Text;

            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserNo) || string.IsNullOrEmpty(UserPass))
            {
                MessageBox.Show("必填项不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (UserID > 0)
            {
                if (UserDb.updateDb(UserID, LableID, UserName, UserNo, UserPass) == 1)
                {
                    i = 1;
                }
            }
            else
            {
                if (UserDb.insertDb(LableID, UserName, UserNo, UserPass) == 1)
                {
                    i = 1;
                }
            }


            if (i == 1)
            {
                DataTable dtTool = null;
                dtTool = UserDb.getAll();
                dgvObject.setDataSource(dtTool);

                //this.Hide();
                this.Close();
            }
        }
    }
}
